package wetalk.software.bupt.com.wetalk.communicate;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;

import com.jyt.message.Message;
import com.jyt.util.MySerializable;

import wetalk.software.bupt.com.wetalk.GloableParams;
import wetalk.software.bupt.com.wetalk.util.SharePreferenceUtil;

/**
 * Created by hp on 2018/1/19.
 */

public class AndroidClientService extends Service{
    private Context context;
    private AndroidClient androidClient;
    private SendBinder sendBinder=new SendBinder();



    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return sendBinder;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        context=this;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        SharePreferenceUtil sharePreferenceUtil=new SharePreferenceUtil(this,"WeTalk_SP");
        final String isLogin=sharePreferenceUtil.getValue("isLogin");
        String userID="";
        if (intent!=null) {
            userID = intent.getStringExtra("userID");
        }
        final String finalUserID = userID;
        new Thread(new Runnable() {
            @Override
            public void run() {
                if(!isLogin.equals("yes")){
                    androidClient=new AndroidClient(GloableParams.ip, "IMServer", finalUserID,context);
                    byte[] bs;
                    bs = MySerializable.object_bytes("connected server");
                    Message checkMsg = new Message(finalUserID,"sys_check","check",bs);
                    androidClient.send(checkMsg);
                    androidClient.work();
                    System.out.println("androidClient  yes  work");
                }else if (androidClient==null){
                    System.out.println("AndroidClientService"+"run: androidclient"+ finalUserID);
                    androidClient=new AndroidClient(GloableParams.ip, "IMServer", finalUserID,context);
                    byte[] bs;
                    bs = MySerializable.object_bytes("connected server");
                    Message checkMsg = new Message(finalUserID,"sys_check","check",bs);
                    androidClient.send(checkMsg);
                    androidClient.work();
                }
            }
        }).start();
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        androidClient=null;

    }

    public class SendBinder extends Binder{


        public void sendMessage(Message message){
            if (androidClient!=null){
                androidClient.send(message);
            }
        }

        public void destroyAndroidClient(){
            Log.d("AndroidClientService","Login关闭service");
            if(androidClient!=null){
                androidClient=null;
            }
        }
    }
}
